package org.goldenport.g3.app

import org.goldenport.g3._
import org.goldenport.g3.messages._
import org.goldenport.g3.messages.datastore._
import org.goldenport.g3.messages.query._
import org.goldenport.g3.events.RecordInserted

/**
 * @since   Sep. 22, 2010
 * @version Jan. 29, 2011
 * @author  ASAMI, Tomoharu
 */
class ReadingLog extends G3Application with UseRecord {
  val kind = 'book
  val schema = Schema(IdField,
                      'name,
                      ('date, XDate),
                      ('comment, XString))

  datastore('db, uri="jdbc:derby:target/g3/derbyDB;create=true",
            driver="org.apache.derby.jdbc.EmbeddedDriver",
            username="user1", password="user1")

  html('viewtop, "Reading Log") {
<body>
<h1>Reading Log Menu</h1>

<ul>
  <li><a g.href="/init">Initialize Reading Log</a></li>
  <li><a g.href="/list">List Readling Log</a></li>
  <li><a g.href="/entry">Write Readling Log</a></li>
</ul>

</body>
  }

  html('viewinit, "Initialize") {
<body>
<h1>Initialize Reading Log</h1>

<ul>
  <li><a g.href="/init/run">OK?</a></li>
  <li><a g.href="/">Return Menu</a></li>
</ul>

</body>
  }

  channel('viewinitrun) invoke("init") agent {
    case _ => Html("Initialize Result", 
<body>
<h1>Initialize Result</h1>

Success!

<ul>
  <li><a g.href="/">Return Menu</a></li>
</ul>

</body>
    )
  }

  channel('viewlist) invoke("list") agent {
    case rs: RecordSet => Html("Reading List",
<body>
<h1>Reading List</h1>

<g.list/>

<ul>
  <li><a g.href="/">Return Menu</a></li>
</ul>

</body>
    ) schema schema += rs
  }

  html('viewentry, "Entry", schema = schema) {
<body>
<h1>Entry</h1>

Please enter book information.

<g.input action="/entry/confirm" label="SUBMIT"/>

<ul>
  <li><a g.href="/">Return Menu</a></li>
</ul>

</body>
  }

  html('viewentryconfirm, "Confirm Entry", schema = schema) {
<body>
<h1>Confirm Entry</h1>

Are you ok?

<g.confirm action="/entry/run" label="SUBMIT"/>

<ul>
  <li><a g.href="/">Return Menu</a></li>
</ul>

</body>
  }

  channel('viewentryrun) invoke("entry") agent {
    case evt: RecordInserted => {
      log_snapshot("evt", Map("evt.records" -> evt.records)) {
      Html("Entry done",
<body>
<h1>Entry</h1>

Reading log is written.

<g.detail/>

<ul>
  <li><a g.href="/">Return Menu</a></li>
</ul>

</body>
    ) schema schema += evt.records
      }
    }
  }

  agents('init)(MsgSeq(Drop(kind), Create(kind, schema))) invoke("db")

  agents('list)(Query(RecordQuery(AllFieldSlot(kind)))) invoke("db")

  agentc('entry) {ctx: G3AgentContext => {
    case post: Post => post.transform(kind, schema, ctx)
  }} invoke("db")

  port("/") invoke("viewtop")
  port("/init") invoke("viewinit")
  port("/init/run") invoke("viewinitrun")
  port("/list") invoke("viewlist")
  port("/entry") invoke("viewentry")
  port("/entry/confirm") invoke("viewentryconfirm")
  port("/entry/run") invoke("viewentryrun")
  port("/service/init") invoke("init")
  port("/service/list") invoke("list")
  port("/service/entry") invoke("entry")
}
